package interview;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author distantstar
 * @version 1.0
 * @date 2021
 * @description interview
 * @since JDK1.8
 *
 * 给定一个包含正整数、加(+)、减(-)、乘(*)、除(/)的算数表达式(括号除外)，计算其结果。
 *
 * 表达式仅包含非负整数，+， - ，*，/ 四种运算符和空格  。 整数除法仅保留整数部分。
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/calculator-lcci
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class Calculator {

    public static int calculate(String s) {
        s = s.replaceAll(" ", "");
        char[] chs = s.toCharArray();
        final Deque<Integer> expression = new ArrayDeque<>();

        for (int i = 0; i < chs.length; i++) {
            StringBuilder builder;
            if (Character.isDigit(chs[i])) {
                builder = new StringBuilder();
                while (i < chs.length && Character.isDigit(chs[i])) {
                    builder.append(chs[i]);
                    i++;
                }
                i--;
                expression.add(Integer.parseInt(builder.toString()));
                continue;
            }

            switch (chs[i]) {
                case '+':
                    i++;
                    builder = new StringBuilder();
                    while (i < chs.length &&Character.isDigit(chs[i])) {
                        builder.append(chs[i]);
                        i++;
                    }
                    i--;
                    expression.add(Integer.parseInt(builder.toString()));
                    break;
                case '-':
                    i++;
                    builder = new StringBuilder();
                    while (i < chs.length &&Character.isDigit(chs[i])) {
                        builder.append(chs[i]);
                        i++;
                    }
                    i--;
                    expression.add(-Integer.parseInt(builder.toString()));
                    break;
                case '*':
                    i++;
                    builder = new StringBuilder();
                    while (i < chs.length &&Character.isDigit(chs[i])) {
                        builder.append(chs[i]);
                        i++;
                    }
                    i--;
                    expression.add(Integer.parseInt(builder.toString()) * expression.pollLast());
                    break;
                case '/':
                    i++;
                    builder = new StringBuilder();
                    while (i < chs.length &&Character.isDigit(chs[i])) {
                        builder.append(chs[i]);
                        i++;
                    }
                    i--;
                    expression.add(expression.pollLast() / Integer.parseInt(builder.toString()));
                    break;
                default:
                    break;
            }
        }

        int res = 0;
        while (!expression.isEmpty()) {
            res += expression.poll();
        }

        return res;
    }

    public static void main(String[] args) {
        System.out.println(calculate(" 3+5 / 2 "));
    }
}
